//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ScientificWorkflow.UIDesigner
{
    #region Using Directives
    using System;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes; 
    #endregion

    public class TextBoxWatermark : TextBox
    {
        /// <summary>
        /// Using a DependencyProperty for the watermark text.
        /// </summary>
        internal static System.Windows.DependencyProperty WatermarkTextProperty =
            System.Windows.DependencyProperty.Register("WatermarkText",
            typeof(string),
            typeof(TextBoxWatermark),
            new System.Windows.FrameworkPropertyMetadata(string.Empty));

        /// <summary>
        /// Using a DependencyProperty for the watermark text.
        /// </summary>
        internal static System.Windows.DependencyProperty WatermarkForegroundProperty =
            System.Windows.DependencyProperty.Register("WatermarkForeground",
            typeof(Brush),
            typeof(TextBoxWatermark),
            new System.Windows.FrameworkPropertyMetadata(Brushes.Gray));

        /// <summary>
        /// Water Mark text Foreground (Default is Brushes.Gray)
        /// </summary>
        public Brush WatermarkForeground
        {
            get { return (Brush)GetValue(WatermarkForegroundProperty); }
            set { SetValue(WatermarkForegroundProperty, value); }
        }

        /// <summary>
        /// Text to be displayed as a water mark. (Default is Please enter username)
        /// </summary>
        public string WatermarkText
        {
            get { return (string)GetValue(WatermarkTextProperty); }
            set { SetValue(WatermarkTextProperty, value); }
        }

        /// <summary>
        /// A flag to specify whether the textbox is watermarked.
        /// </summary>
        public bool IsWatermarked { get; set; }
     
        /// <summary>
        /// Textbox with a watermark background.
        /// </summary>
        public TextBoxWatermark():base()
        {
           this.Initialized += new EventHandler(TextBoxWatermark_Loaded);
        }

        /// <summary>
        /// Clear the texteBox data and then apply watermark.
        /// </summary>
        public void ClearText()
        {
            this.Watermark();
        }

        /// <summary>
        /// The loaded event for the textbox.
        /// </summary>
        /// <param name="sender">source</param>
        /// <param name="e">event args.</param>
        void TextBoxWatermark_Loaded(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.Text))
            {
                this.Watermark();
            }
        }

        /// <summary>
        /// Check if watermark is displayed and swap over to standard background. 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnGotKeyboardFocus(KeyboardFocusChangedEventArgs e)
        {
            if (this.Text == this.WatermarkText && this.Foreground== this.WatermarkForeground)
            {
                this.RemoveWatermark();
            }
  
            base.OnGotKeyboardFocus(e);
        }

        /// <summary>
        /// Check if watermark should be displayed if so setup. 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLostKeyboardFocus(KeyboardFocusChangedEventArgs e)
        {
            this.Text = this.Text.Trim();
            if (this.Text.Length == 0)
            {
                this.Watermark();
            }
            base.OnLostKeyboardFocus(e);
        }
        
        /// <summary>
        /// Sets up a watermark on the textbox. 
        /// </summary>
        private void Watermark()
        {
            this.IsWatermarked = true;
            this.Foreground = this.WatermarkForeground;
            this.Text = this.WatermarkText;
        }

        /// <summary>
        /// Removes watermark on the textbox. 
        /// </summary>
        private void RemoveWatermark()
        {
            this.IsWatermarked = false;
            this.Text = string.Empty;
            this.Foreground = Brushes.Black;
        }
    }
}


